﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;

namespace PeisongWeb.admin
{
    public partial class CurrentCostProductEdit :adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CE", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void drpBind()
        {
            drp_Type.DataSource = logic.category.select("treelevel=1", "sort", 200, 1).Tables[1];// logic.category.list();
            drp_Type.DataBind();
            drp_Type.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择类别--", ""));
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (txt_proNo.Text.Trim() != "")
                where.AppendFormat("and productNo ='{0}'", txt_proNo.Text.Trim());
            if (txt_proName.Text.Trim() != "")
                where.AppendFormat("and name like '%{0}%'", txt_proName.Text.Trim());
            if (drp_Type.SelectedValue.Trim() != "")
                where.AppendFormat("and categoryId = {0}", drp_Type.SelectedValue.Trim());
            //DataSet ds = logic.product.listWithCurrentcost(where.ToString(), "productId", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            DataSet ds = logic.product.listWithCurrentcost(where.ToString(), "productId", 2000,1);
            //AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            if (ds.Tables[1].Rows.Count == 0)
                btn_quoted.Visible = false;
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        //protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        //{
        //    rptBind();
        //}
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void btn_quoted_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpt_list.Items.Count; i++)    //为时价商品报价
            {
                RepeaterItem row = rpt_list.Items[i];
                HiddenField hid = row.FindControl("hid_proId") as HiddenField;
                TextBox txtSellerPrice = row.FindControl("txt_sellerPrice") as TextBox;
                HtmlInputHidden hidNature = row.FindControl("hid_nature") as HtmlInputHidden;
                if (txtSellerPrice.Text.Trim() != "")
                {
                    int proid = Convert.ToInt32(hid.Value);
                    decimal sellerPrice = Convert.ToDecimal(txtSellerPrice.Text);
                    logic.orderItem.updateCurrentCost(proid, sellerPrice, logic.sysAdmin.AdminID, hidNature.Value);
                }
            }
            Library.Script.ClientMsg("修改成功，请核对订单信息！");
            rptBind();
        }
    }
}
